package com.example.demo.solution;

import java.util.Arrays;

/**
 * @desc: 移动零<br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年10⽉21⽇<br/>
 * @version：v1.0.0<br>
 */
public class MoveZeros {

    public static void main(String[] args) {
        int[] arr = {0, 1, 0, 23, 12};
        int[] move = move(arr);
        System.out.println(Arrays.toString(move));
    }

    public static int[] move(int[] arr) {
        if (arr.length == 0) {
            return arr;
        }
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                arr[index] = arr[i];
                if (index != i) {
                    arr[i] = 0;
                }
                index++;
            }
        }
        return arr;
    }
}
